package com.apexedu.biz.action;

import com.apexedu.biz.entity.TServiceCode;
import com.apexedu.framework.service.BaseService;
import com.apexedu.sys.action.AbstractBaseActionSupport;
import com.apexedu.sys.entity.ApplicationEnum;
import org.apache.commons.lang3.StringUtils;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;


@SuppressWarnings("serial")
@Component
@Scope("prototype")
public class ServiceCodeAction extends AbstractBaseActionSupport {

	/**
	 * 此方法植入service类
	 */
	@Resource(name="serviceCodeService")
	public void setBaseService(BaseService baseService){
		super.setBaseService(baseService);
	}

	private TServiceCode tserviceCode;

	public TServiceCode getTserviceCode() {
		return tserviceCode;
	}

	public void setTserviceCode(TServiceCode tserviceCode) {
		this.tserviceCode = tserviceCode;
	}

	@SuppressWarnings("unchecked")
	public List<ApplicationEnum> getSuperCode(String zdlb, String zdbm){
		List<ApplicationEnum> list = Collections.EMPTY_LIST;
		if(StringUtils.isNotBlank(zdlb)){
			if(StringUtils.isBlank(zdbm)){
				//第一次新增子字典
				String hql = "select new ApplicationEnum(zdbm, zdmc) from TSysCode where zdlb=? and sfsy=1 " +
						"and sjdm is null order by zdbm, length(zdbm)";
				list = getApplicationEnumService().getApplicationEnums(false, hql, zdlb);
			}else{
				String hql = "select new ApplicationEnum(zdbm, zdmc) from TSysCode where zdlb=? and zdbm!=? " +
						"and sfsy=1 and sjdm is null order by zdbm, length(zdbm)";
				list = getApplicationEnumService().getApplicationEnums(false, hql, zdlb, zdbm);
			}
		}
		return list;
	}

	/**
	 * 系统字典类别
	 *
	 * @return
	 */
	public List<ApplicationEnum> getSysCodeSort() {

		String hql = "select new ApplicationEnum(t.zdlb,t.lbmc) from TServiceCodesort t group by t.zdlb,t.lbmc order by t.zdlb";
		return applicationEnumService.getApplicationEnums(true, hql);
	}
}
